/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-22 15:40:56
 * @filename : static/nmm/src/views/changePassword.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){

    require('{component}promptTip/promptTip.css');
    var CgPWView,
        mId = 'changePassword',reRender = false,menuName = 'changePassword',
        imglazyload = require('{component}imglazyload/zepto.imglazyload'),
        promptTip = require('{component}promptTip/promptTip'),
        panel       = require('{component}panel/zepto.panel'),
        tplN        = require('{view}userNav.tpl'),
        tpl         = require('{view}changePassword.tpl');

    CgPWView = Backbone.View.extend({

        el: 'body',

        template: _.template(tpl),
        templateN: _.template(tplN),

        initialize: function () {},

        render: function (obj) {
            if (!share.checkPermission(true)){return;};
            share.reRender(reRender,mId);
            if ($('#'+mId).length < 1) { 
                $('body').append(this.template(lang));
                this.bindEvt();  
            } else {
                $('#'+mId).css('display','block');
            }
            share.panel(panel,this.templateN,mId,menuName);
            this.clearInput(); 
        },

        bindEvt : function() {
            var dom = $('#'+mId),tthis = this;
            dom.find('.back').on('tap',function(){
                tthis.goBack();
            });
            dom.find('.btn-changepw').on('tap',function(){
                tthis.changePw();
            });
        },

        changePw : function() {
            var obj = {};
            obj.original_password = $('input[name="passwordO"]').val();
            obj.password = $('input[name="passwordN"]').val();
            obj.password_repeat = $('input[name="passwordC"]').val();
            if (this.validata(obj)) {
                this.ajaxChangePw(obj);
            }
        },

        goBack : function() {
             //window.location.href = '#signin/whole'; 
            window.history.go(-1); 
        },

        validata : function(data) {
            var bool = true;
            for (var i in data) {
                var obj={bool:true,type:1};
                if ($.trim(data[i]) == '') {
                    obj.bool = false;
                    obj.msg = lang.required; 
                } else if (!util.isLength(data[i],6,18)) {
                    obj.bool = false;
                    obj.msg = lang.password_format_error;
                } else if (i == 'password_repeat'){
                    if(data['password_repeat'] !== data['password']) {
                        obj.bool = false;
                        obj.msg = lang.passwordConfirm;
                    }
                };
                obj.dom = $('#'+mId).find('.'+i);
                share.inputError(obj);
                if (!obj.bool) {
                    bool = false;
                }
            }
            return bool;
        },

        clearInput : function() {
            var dom = $('#'+mId).find('.signup_item');
            dom.find('input').val('');
            if (dom.next().hasClass('err')) {
                dom.next().remove();
                dom.find('input').removeAttr('style').removeClass('input_err');
            }
 
        },
 
        ajaxChangePw : function(data) {
            var dom = $('.btn-changepw');
                dom.attr('ajax',1);
            share.ajaxStartA(dom);
            var obj = {
                url : seajs.data.vars.apiUrl+share.getSessionId()+'/reset_password',
                type : 'POST',
                data : data,
                success : function(data) {
                    if (data.errcode) {
                        var obj = {type:3}; 
                        obj.bool = false;
                        obj.msg = data.errmsg;
                        obj.dom = $('#'+mId).find('.password_repeat');
                        share.inputError(obj);
                    } else {
                        promptTip.show({type:'success',text:'Password successfully changed.',dom:$('#'+mId).find('.promptTip'),time:2000});                           
                        setTimeout(function() {
                           share.loginOut(); 
                           window.location.href = '#signin/whole'; 
                        },2000);
                    }
                },
                error : function(xhr, errorType, error) {
                    share.error('alert','http request failed!'); 
                },
                complete : function(xhr,status){
                    dom.attr('ajax',0);
                    share.ajaxEndA(dom);
                } 
            };
            $.ajax(share.ajaxControl(obj)); 
        }
 
    });

    module.exports = CgPWView;
});

